package com.medic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.medic.entity.group.pain.Compliance;
import com.medic.entity.group.pain.Health;
import com.medic.entity.group.pain.Medic;
import com.medic.entity.group.pain.Pain;
import com.medic.entity.group.pain.Reaction;
import com.medic.entity.group.pain.SikerInfo;
import com.medic.entity.group.pain.TakeRecord;
import com.medic.entity.group.pain.Task;
import com.medic.entity.group.pain.TitrRecord;
import com.medic.mapper.interf.TaskMapper;
import com.medic.service.interf.TaskServiceInterface;
import com.medic.util.CacheUtil;
import com.medic.util.StringUtil;

/**
 * 任务业务类（暂时不允许删除）
 * 
 * @author Mr.zhang
 *
 */
@Service("taskService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class TaskServiceImpl implements TaskServiceInterface {

	@Autowired
	private TaskMapper taskMapper;

	@Override
	public boolean save(Task object) throws Exception {
		boolean result = false;
		if (taskMapper.save(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean delete(Task object) throws Exception {
		return false;
	}

	@Override
	public boolean update(Task object) throws Exception {
		return false;
	}

	@Override
	public Page<Task> getByPageUser(int current, int sikerId, int groupId, int status, long crDate) throws Exception {
		PageHelper.startPage(current, CacheUtil.getTask_pagesize(), "tsId desc");
		Page<Task> page = (Page<Task>) taskMapper.selectByPageUser(sikerId, groupId, status, crDate);
		return page;
	}

	@Override
	public Page<Task> getByPageDoctor(int current, int groupId, int status, long crDate) throws Exception {
		PageHelper.startPage(current, CacheUtil.getTask_pagesize(), "tsId desc");
		Page<Task> page = (Page<Task>) taskMapper.selectByPageDoctor(groupId, status, crDate);
		return page;
	}

	@Override
	public Task getById(long tsId) throws Exception {
		return taskMapper.selectById(tsId);
	}

	@Override
	public boolean saveCompliance(Compliance compliance) throws Exception {
		boolean result = false;
		if (compliance.getTsId() > 0 && taskMapper.saveCompliance(compliance) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public Compliance getComplianceByTsId(long tsId) throws Exception {
		return taskMapper.selectComplianceByTsId(tsId);
	}

	@Override
	public int saveHealth(List<Health> hls) throws Exception {
		for (Health h : hls) {
			if (h.getTsId() <= 0) {
				return 0;
			}
		}
		return taskMapper.saveHealth(hls);
	}

	@Override
	public List<Health> getHealthByTsId(long id) throws Exception {
		return taskMapper.selectHealthByTsId(id);
	}

	@Override
	public boolean savePain(Pain pain) throws Exception {
		boolean result = false;
		if (pain.getTsId() > 0 && taskMapper.savePainNature(pain.getPn()) > 0 && taskMapper.savePain(pain) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public Pain getPainByTsIdOrId(long tsId, int painId) throws Exception {
		return taskMapper.selectPainByTsIdOrId(tsId, painId);
	}

	@Override
	public boolean saveReaction(Reaction reaction) throws Exception {
		boolean result = false;
		if (reaction.getTsId() > 0 && taskMapper.saveReaction(reaction) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public Reaction getReactionByTsId(long tsId) throws Exception {
		return taskMapper.selectReactionByTsId(tsId);
	}

	@Override
	public boolean finsh(long tsId, int sikerId) throws Exception {
		boolean result = false;
		if (taskMapper.finish(tsId, sikerId, new Date().getTime()) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean checkup(int status, long tsId) throws Exception {
		boolean result = false;
		if (taskMapper.checkup(status, tsId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean updateTitrCrDate(long crDate, long tsId) throws Exception {
		boolean result = false;
		if (taskMapper.updateTitrCrDate(crDate, tsId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean saveTitrRecord(TitrRecord titr) throws Exception {
		boolean result = false;
		if (taskMapper.saveTitrRecord(titr) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public List<TitrRecord> getTitrRecordByTsId(long tsId) throws Exception {
		return taskMapper.selectTitrRecordByTsId(tsId);
	}

	@Override
	public boolean sureTake(long titrId) throws Exception {
		boolean result = false;
		if (taskMapper.sureTake(titrId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean updateTitrRecord(long titrId, int painId, int painLevel) throws Exception {
		boolean result = false;
		if (taskMapper.updateTitrRecord(titrId, painId, new Date().getTime(), painLevel) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean saveTakeRecord(TakeRecord takeRecord) throws Exception {
		boolean result = false;
		if (taskMapper.saveTakeRecord(takeRecord) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public TakeRecord getTakeRecordByTsId(long tsId) throws Exception {
		return taskMapper.selectTakeRecordByTsId(tsId);
	}

	@Override
	public Page<Task> getByPageAssitant(int current, int groupId, int status, long crDate) throws Exception {
		PageHelper.startPage(current, CacheUtil.getTask_pagesize(), "tsId desc");
		Page<Task> page = (Page<Task>) taskMapper.selectByPageAssitant(groupId, status, crDate);
		return page;
	}

	@Override
	public Page<Task> getByPagePharmacist(int current, int groupId, int status, long crDate) throws Exception {
		PageHelper.startPage(current, CacheUtil.getTask_pagesize(), "tsId desc");
		Page<Task> page = (Page<Task>) taskMapper.selectByPagePharmacist(groupId, status, crDate);
		return page;
	}

	@Override
	public int judge(long tsId, int sikerId) throws Exception {
		return taskMapper.judge(tsId, sikerId);
	}

	@Override
	public List<Task> getUndo(long nowDate) throws Exception {
		return taskMapper.selectUndo(nowDate);
	}

	@Override
	public long getTitrTask(int sikerId) throws Exception {
		return taskMapper.selectTitrTask(sikerId);
	}

	@Override
	public TitrRecord saveTitrRecord(SikerInfo info, double amounts) throws Exception {
		TitrRecord titr = null;
		if (info.getIsTitr() == 2) {
			titr = new TitrRecord();
			String medicName = "吗啡";
			// 非耐受
			if (info.getIsTolerance() == 1) {
				switch (info.getTitrNum()) {
				case 0:
					amounts = 10;
					medicName = StringUtil.MEDIC_NAME;
					break;
				case 1:
					if (info.getPainLevel() >= 4 && info.getPainLevel() < 7) {
						amounts = 10;
					} else if (info.getPainLevel() >= 7) {
						amounts = 15;
					}
					break;
				default:
					if (info.getPainLevel() >= 7) {
						amounts = amounts * 1.5;
					}
					break;
				}
			} else {
				switch (info.getTitrNum()) {
				case 0:
					for (Map.Entry<Integer, Medic> entry : CacheUtil.getMedics().entrySet()) {
						Medic medic = entry.getValue();
						if (medic.getSikerId() == info.getSikerId()
								&& medic.getMedicName().equals(StringUtil.MEDIC_NAME)) {
							amounts = medic.getAlert().get(0).getAmounts();
							break;
						}
					}
					medicName = StringUtil.MEDIC_NAME;
					break;
				case 1:
					if (info.getPainLevel() >= 4 && info.getPainLevel() < 7) {
						amounts = 10;
					} else if (info.getPainLevel() >= 7) {
						amounts = 15;
					}
					break;
				default:
					if (info.getPainLevel() >= 7) {
						amounts = amounts * 1.5;
					}
					break;
				}
			}
			titr.setAmounts(amounts);
			titr.setMedicName(medicName);
			titr.setTsId(info.getTrTsId());
			titr.setTakeway("口服");
			taskMapper.saveTitrRecord(titr);
		}
		return titr;
	}

	@Override
	public int getTaskNum(int sikerId, int groupId, int tsType) throws Exception {
		return 0;
	}

	@Override
	public int saveBatch(List<Task> tasks) throws Exception {
		return taskMapper.saveBatch(tasks);
	}
}
